//
//  DHSearchSizeCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/2/10.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchSizeCell.h"
#import "DHSearchSizeItemView.h"

@interface DHSearchSizeCell()

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) NSArray *dataArr;

@end

@implementation DHSearchSizeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!self.scrollView) {
            self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 54)];
            [self.contentView addSubview:self.scrollView];
        }
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 53, kScreenWidth, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"#ECF2F1"];
        [self.contentView addSubview:line];
    }
    return self;
}

- (NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}

- (void)layoutSubviews{
    self.scrollView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)updateCellWithConditionArr:(NSArray *)conditionArr firstLoad:(BOOL)firstLoad{
    self.dataArr = conditionArr;
    if (firstLoad) {
        CGFloat W = 92;
        [self.scrollView removeAllSubviews];
        __weak __typeof(self)weakSelf = self;
        self.scrollView.contentSize = CGSizeMake(self.dataArr.count * W, self.height);
        [self.dataArr enumerateObjectsUsingBlock:^(NSString *sizeName, NSUInteger idx, BOOL * _Nonnull stop) {
            DHSearchSizeItemView *sizeItemView = [[DHSearchSizeItemView alloc] initWithFrame:CGRectMake(idx * W, 0, W, weakSelf.height) andSceneTitle:sizeName];
            [weakSelf.scrollView addSubview:sizeItemView];
            [sizeItemView.sizeButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            sizeItemView.sizeButton.tag = idx;
        }];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)clickAction:(UIControl *)sender{
    for (DHSearchSizeItemView *item in self.scrollView.subviews) {
        if ([item isKindOfClass:[DHSearchSizeItemView class]]) {
            if (item.sizeButton.tag != sender.tag) {
                item.sizeButton.selected = YES;
                [item clickAction:item.sizeButton];
            }
        }
    }
    NSString *sizeName = self.dataArr[sender.tag];
    if (sender.isSelected) {
        [self routerEventWithName:@"DHSearchSizeCell" userInfo:@{@"size_ame":sizeName}];
    }else{
        [self routerEventWithName:@"DHSearchSizeCell" userInfo:@{@"size_ame":[NSNull null]}];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
